<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://d3js.org/d3.v5.min.js"></script>
  <style>
    #app {
      width: 500px;
      height: 400px;
      background: #efefef;
      position: relative;
    }

    .bar {
      width: 50px;
      fill: green;
      position: absolute;
      bottom: 100px;
    }

    .bar span {
      display: block;
      text-align: center;
    }
  </style>
</head>

<body>
  <svg id="app">
  </svg>

  <script>

    var datalist = [10, 20, 30, 40, 50]
    var container = d3.select("#app");

    container.selectAll('rect')
      .data(datalist)
      .enter()
      .append('rect')
      .classed('bar', true)
      .style('height', function (d, i) {
        return d * 5 + 'px';
      })
      .attr('x', function (d, i) {
        return i * 60 + 50 + 'px'
      })
      .attr('y', function (d, i) {
        return 400 - d * 5 - 50+ 'px';
      });


    container.selectAll('text').data(datalist)
      .enter()
      .append('text')
      .attr('text-anchor','middle')
      .text(function (d) {
        return d;
      })
      .attr('x', function (d, i) {
        return i * 60 + 23 + 50;
      })
      .attr('y', function (d, i) {
        return 400 - d * 5 + 20 -50;
      })
      .style('fill', function(d){
        if(d>30){
          return 'red'
        }
      })




  </script>

</body>

</html>